import Vue from 'vue'
import Router from 'vue-router'
/* eslint-disable */
const routerPush = Router.prototype.push
Router.prototype.push = function push(location) {
    return routerPush.call(this, location).catch(error => error)
}

const VueRouterReplace = Router.prototype.replace
Router.prototype.replace = function replace(to) {
    return VueRouterReplace.call(this, to).catch(err => err)
}

Vue.use(Router)

const routes = [{
        path: '/',
        redirect: '/home'
    },
    {
        name: 'home',
        path: '/home',
        component: () =>
            import ('@/pages/home/home'),
        meta: {
            title: '首页'
        }
    },
    {
        name: 'user',
        path: '/user',
        component: () =>
            import ('@/pages/user/user'),
        meta: {
            title: '会员中心'
        }
    },
    {
        name: 'cart',
        path: '/cart',
        component: () =>
            import ('@/pages/cart/cart'),
        meta: {
            title: '购物车',
            requireAuth: true
        }
    },
    {
        name: 'login',
        path: '/login',
        component: () =>
            import ( /* webpackChunkName: "login" */ '@/pages/login/login'),
        meta: {
            title: '登录/注册'
        }
    },
    {
        name: 'Comment',
        path: '/comment',
        component: () =>
            import ('./pages/comment/index.vue'),
        meta: {
            title: '登录/注册'
        }
    },
    {
        name: 'goods-detail',
        path: '/goods-detail/:id',
        component: () =>
            import ('@/pages/goods-detail/goods-detail'),
        meta: {
            title: '商品详情'
        }
    },
    {
        name: 'goods-reputation',
        path: '/goods-reputation',
        component: () =>
            import ('@/pages/goods-reputation/goods-reputation'),
        meta: {
            title: '全部评价'
        }
    },
    {
        name: 'order-submit',
        path: '/order-submit',
        component: () =>
            import ('@/pages/order-submit/order-submit'),
        meta: {
            title: '提交订单'
        }
    },
    {
        name: 'order-list',
        path: '/order-list',
        component: () =>
            import ('@/pages/order-list/order-list'),
        meta: {
            title: '订单列表',
            requireAuth: true
        }
    },
    {
        name: 'order-detail',
        path: '/order-detail',
        component: () =>
            import ('@/pages/order-detail/order-detail'),
        meta: {
            title: '订单详情',
            requireAuth: true
        }
    },
    {
        name: 'address-list',
        path: '/address-list',
        component: () =>
            import ('@/pages/address-list/address-list'),
        meta: {
            title: '管理收货地址',
            requireAuth: true
        }
    },
    {
        name: 'address-edit',
        path: '/address-edit',
        component: () =>
            import ('@/pages/address-edit/address-edit'),
        meta: {
            title: '管理收货地址',
            requireAuth: true
        }
    },
    {
        name: 'setting',
        path: '/setting',
        component: () =>
            import ('@/pages/user/setting'),
        meta: {
            title: '我的资料',
            requireAuth: true
        }
    },
    {
        name: '404',
        path: '/404',
        component: () =>
            import ('@/pages/error-page/404'),
        meta: {
            title: '404'
        }
    },
    { path: '*', redirect: '/404', hidden: true }
]

const router = new Router({
    mode: 'history',
    routes,
    scrollBehavior(to, from, savedPosition) {
        if (savedPosition) {
            return savedPosition
        } else {
            return { x: 0, y: 0 }
        }
    }
})

// add route title
router.beforeEach((to, from, next) => {
    const title = to.meta && to.meta.title
    if (title) {
        document.title = title
    }
    next()
})

export default router